package com.huike.business.service.impl;


import com.huike.business.domain.TbBusiness;
import com.huike.business.domain.TbBusinessTrackRecord;
import com.huike.business.domain.vo.BusinessTrackVo;
import com.huike.business.mapper.TbBusinessMapper;
import com.huike.business.mapper.TbBusinessTrackRecordMapper;
import com.huike.business.service.ITbBusinessTrackRecordService;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.utils.SecurityUtils;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 商机跟进记录Service业务层处理
 * 
 * @author wgl
 * @date 2021-04-28
 */
@Service
public class TbBusinessTrackRecordServiceImpl implements ITbBusinessTrackRecordService {


    @Autowired
    private TbBusinessMapper tbBusinessMapper;

    @Autowired
    private TbBusinessTrackRecordMapper tbBusinessTrackRecordMapper;


    /**
     * 商机管理--商机跟进记录
     * @param id 商机id
     * @return
     */
    @Override
    public List<TbBusinessTrackRecord> list(Long id) {
        return tbBusinessMapper.list(id);
    }

    /**
     * 新增商机跟进记录
     * @param businessTrackVo 商机记录实体类
     * @return
     */
    @Override
    public AjaxResult add(BusinessTrackVo businessTrackVo) {
        try {
            //1.获取当前用户名
            String username = SecurityUtils.getUsername();
            businessTrackVo.setCreateBy(username);
            //2.保存商机信息
            TbBusiness tbBusiness = new TbBusiness();
            tbBusiness.setId(businessTrackVo.getBusinessId());
            BeanUtils.copyProperties(businessTrackVo,tbBusiness);
            tbBusinessMapper.updateTbBusiness(tbBusiness);
            //3.保存商机跟进记录
            TbBusinessTrackRecord tbBusinessTrackRecord = new TbBusinessTrackRecord();
            BeanUtils.copyProperties(businessTrackVo,tbBusinessTrackRecord);
            tbBusinessTrackRecordMapper.add(tbBusinessTrackRecord);
        } catch (BeansException e) {
            e.printStackTrace();
            return  null;
        }
        return AjaxResult.success();
    }
}
